/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

var RegisterViewModel = function () 
	{
    var self = this;
	// put a property loop here
	self.UserName = ko.observable();
	self.Password = ko.observable();
	self.ConfirmPassword = ko.observable();
	self.Email = ko.observable();
	self.FirstName = ko.observable();
	self.LastName = ko.observable();
	self.Address = ko.observable();
	self.City = ko.observable();
	self.Region = ko.observable();
	self.PostalCode = ko.observable();
	self.OfficePhone = ko.observable();
	self.CellPhone = ko.observable();
	self.PasswordQuestion = ko.observable();
	self.PasswordAnswer = ko.observable();
	self.Title = ko.observable();
	self.PasswordQuestionList = ko.observableArray([]);
	self.GetPasswordQuestions = ko.observableArray([]);

	self.selectedVal = ko.observableArray([]);
	self.passQS = ko.observableArray([]);
			
    
    self.RegisterList = ko.observableArray([]);
    self.setApplication = function (
			_UserName, _Password, _ConfirmPassword, _Email, _FirstName, _LastName, _Address, _City, _Region, _PostalCode, _OfficePhone, _CellPhone, _PasswordQuestion, _PasswordAnswer, _Title
	
	) {

		UserName = _UserName;
		Password = _Password;
		RememberMe = _RememberMe;
		ConfirmPassword = _ConfirmPassword;
		Email = _Email;
		FirstName = _FirstName;
		LastName = _LastName;
		Address = _Address;
		City = _City;
		Region = _Region;
		PostalCode = _PostalCode;
		OfficePhone = _OfficePhone;
		CellPhone = _CellPhone;
		PasswordQuestion = _PasswordQuestion;
		PasswordAnswer = _PasswordAnswer;
		Title = _Title;
			
        
    }; //  End of set procedure 

	self.Register = function (app) {
        //validation
	    $(".form-register").validate({
	       
	    });
	    if (!$(".form-register").valid()) {
	        //alert("Invalied");
	        return;
	    }
	    self.Action = "Add";
	    self.Caller = "trooks";
	    self.Role = "user";

	    var myData = ko.toJSON({ Action: self.Action, Caller: self.Caller, UserName: self.UserName, Password: self.Password, ConfirmPassword: self.ConfirmPassword, Email: self.Email, FirstName: self.FirstName, LastName: self.LastName, Address: self.Address, City: self.City, Region: self.Region, PostalCode: self.PostalCode, OfficePhone: self.OfficePhone, CellPhone: self.CellPhone, PasswordQuestion: self.selectedVal().PasswordQuestionID, PasswordAnswer: self.PasswordAnswer, Title: self.Title });
        jQuery.ajax({
            url: "./account/register/",
            type: 'post',
            data: myData,
            contentType: 'application/json',
            success: function (result) {
                //location.href = "./";
                $('#success-dis').text('Your account has been created Successfully. Please fallow the link in email to activate.... ');
                $('#register-error').css({ 'display': 'none' });
                $('#success-msg').css({ 'display': 'block' });
                $(document).scrollTop(0);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#error-dis').text(thrownError);
                $('#register-error').css({ 'display': 'block' });
                $(document).scrollTop(0);
	    }
        });
        $("#registerForm")[0].reset();
	}; //End of add procedure

	self.getPassQs = function () {
	    var self = this;
	    self.Action = "GetAll";
	    self.Caller = "trooks";
	    self.Role = "user";
	    self.PageNumber = "1";
	    self.PageSize = "10";
	    self.SortColumn = "PasswordQuestionID";
	    self.SortType = "Asc";


	    jQuery.ajax({
	        url: "./api/PasswordQuestionAPI/",
	        type: 'get',
	        data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
	    }).done(function (data) {	        	       
	        $.each(data, function (key, val) {
	            self.passQS.push(new passItem(val.PasswordQuestionID, val.PasswordQuestionDesc));	            
	        })	     
	    });	       
	};

	function passItem(id, dis) {
	    this.PasswordQuestionID = ko.observable(id);
	    this.PasswordQuestionDesc = ko.observable(dis);

	}
}; //End of view model






